package com.wjh.yygh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wjh.yygh.model.hosp.Department;
import com.wjh.yygh.resopitory.DeparentmentRespository;
import com.wjh.yygh.service.DepartmentService;
import com.wjh.yygh.vo.hosp.DepartmentQueryVo;
import com.wjh.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DeparentmentRespository deparentmentRespository;

    /**
     * 上传科室信息
     * @param paramMap
     */
    @Override
    public void save(Map<String,Object> paramMap) {

        String s1 = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(s1, Department.class);

        Department departmentsExist = deparentmentRespository.getDepartmentsByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());

        if (!StringUtils.isEmpty(departmentsExist)){
            departmentsExist.setUpdateTime(new Date());
            departmentsExist.setIsDeleted(0);
            deparentmentRespository.save(departmentsExist);
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            deparentmentRespository.save(department);
        }

    }

    /**
     * 查询科室
     * @param page
     * @param limit
     * @param departmentQueryVo
     * @return
     */
    @Override
    public Page<Department> findDepartment(int page, int limit,DepartmentQueryVo departmentQueryVo) {
        Pageable pageable = PageRequest.of(page-1,limit);

        ExampleMatcher matching = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase();

        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        Example<Department> of = Example.of(department, matching);

        Page<Department> all = deparentmentRespository.findAll(of, pageable);
        return all;
    }

    /**
     * 删除科室
     * @param hoscode
     * @param depcode
     */
    @Override
    public void remove(String hoscode, String depcode) {

        Department departmentsByHoscodeAndDepcode = deparentmentRespository.getDepartmentsByHoscodeAndDepcode(hoscode, depcode);
        if (!StringUtils.isEmpty(departmentsByHoscodeAndDepcode)){
            deparentmentRespository.deleteById(departmentsByHoscodeAndDepcode.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDepartTree(String hoscode) {
        ArrayList<DepartmentVo> departmentVos = new ArrayList<>();
        //所有科室信息
        List<Department> departmentsByHoscode = deparentmentRespository.getDepartmentsByHoscode(hoscode);

        //根据大科室分组
        Map<String, List<Department>> departmentMap
                = departmentsByHoscode.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历集合
//        Map<Map.Entry<String,List<Department>>,>
        for (Map.Entry<String,List<Department>> entry : departmentMap.entrySet()){
            String key = entry.getKey();
            List<Department> departmentList = entry.getValue();

            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(key);
            departmentVo.setDepname(departmentList.get(0).getDepname());
            ArrayList<DepartmentVo> departmentVoChildren = new ArrayList<>();
            departmentList.stream().forEach(department -> {
                DepartmentVo departmentVo1 = new DepartmentVo();
                departmentVo1.setDepcode(department.getDepcode());
                departmentVo1.setDepname(department.getDepname());
                departmentVoChildren.add(departmentVo1);
            });
            departmentVo.setChildren(departmentVoChildren);
            departmentVos.add(departmentVo);
        }
        return departmentVos;
    }

    @Override
    public String getDepName(String hoscode,String depcode) {
        return deparentmentRespository.getDepartmentsByHoscodeAndDepcode(hoscode,depcode).getDepname();
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return deparentmentRespository.getDepartmentsByHoscodeAndDepcode(hoscode,depcode);
    }


}
